package com.yike.product.controller;

import com.yike.product.entity.Category;
import com.yike.product.param.CategoryParam;
import com.yike.product.service.CategoryService;
import com.yike.product.vo.CategoryVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.product.param.CategoryPageParam;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Add;
import com.yike.common.api.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 商品分类 控制器
 *
 * @author Sakura
 * @since 2024-09-02
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Module("product")
@Api(value = "商品分类API", tags = {"商品分类管理"})
@ApiIgnore
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加商品分类
     */
    @PostMapping("/add")
    @OperationLog(name = "添加商品分类", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商品分类", response = ApiResult.class)
    public ApiResult<Boolean> addCategory(@Validated(Add.class) @RequestBody CategoryParam categoryParam) throws Exception {
        boolean flag = categoryService.saveCategory(categoryParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改商品分类
     */
    @PostMapping("/update")
    @OperationLog(name = "修改商品分类", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商品分类", response = ApiResult.class)
    public ApiResult<Boolean> updateCategory(@Validated(Update.class) @RequestBody CategoryParam categoryParam) throws Exception {
        boolean flag = categoryService.updateCategory(categoryParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除商品分类
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除商品分类", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商品分类", response = ApiResult.class)
    public ApiResult<Boolean> deleteCategory(@PathVariable("id") Long id) throws Exception {
        boolean flag = categoryService.deleteCategory(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取商品分类详情
     */
    @GetMapping("/info/{id}")
    //@OperationLog(name = "商品分类详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商品分类详情", response = Category.class)
    public ApiResult<Category> getCategory(@PathVariable("id") Long id) throws Exception {
        Category category = categoryService.getById(id);
        return ApiResult.ok(category);
    }

    /**
     * 商品分类分页列表
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "商品分类分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商品分类分页列表", response = CategoryVo.class)
    public ApiResult<Paging<CategoryVo>> getCategoryPageList(@Validated @RequestBody CategoryPageParam categoryPageParam) throws Exception {
        Paging<CategoryVo> paging = categoryService.getCategoryPageList(categoryPageParam);
        return ApiResult.ok(paging);
    }

}

